﻿package mortal.game.resource
{
    import com.gengine.debug.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import flash.system.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.common.preLoadPage.*;

    public class ResConfig extends Object
    {
        private var _effectMap:Dictionary;
        private var _index:int;
        private var _length:int;
        private var _maxLength:int;
        private var _allBoss:Array;
        private var _allNpc:Array;
        public static var isNewInit:Boolean = true;
        private static var _instance:ResConfig;

        public function ResConfig()
        {
            this._effectMap = new Dictionary();
            this._allBoss = [];
            this._allNpc = [];
            if (_instance != null)
            {
                throw new Error(" ResConfig 单例 ");
            }
            if (isNewInit)
            {
                this.init();
            }
            return;
        }// end function

        public function initForSceneEditor() : void
        {
            this.init(false);
            return;
        }// end function

        private function init(param1:Boolean = true) : void
        {
            var _loc_6:Object = null;
            var _loc_2:* = System.totalMemory;
            this.initEffect();
            var _loc_3:* = getTimer();
            var _loc_4:* = ConfigManager.instance.getByteArrayByFileName("assets.vas");
            if (param1)
            {
                _loc_4.uncompress();
            }
            Log.debug("解压assets.vas耗时", getTimer() - _loc_3);
            _loc_4.position = 0;
            var _loc_5:* = _loc_4.readObject() as Object;
            for each (_loc_6 in _loc_5)
            {
                
                break;
            }
            if (_loc_6 is Array)
            {
                this.newWrite(_loc_5);
            }
            else
            {
                this.write(_loc_5);
            }
            Log.system("系统总内存:", System.totalMemory, "解压Res内存增加:", System.totalMemory - _loc_2);
            return;
        }// end function

        private function initEffect() : void
        {
            var _loc_3:String = null;
            var _loc_1:* = ConfigManager.instance.getByteArrayByFileName("effect.vas");
            _loc_1.uncompress();
            _loc_1.position = 0;
            var _loc_2:* = _loc_1.readObject() as Object;
            for (_loc_3 in _loc_2)
            {
                
                this._effectMap[_loc_3] = _loc_2[_loc_3];
            }
            return;
        }// end function

        protected function write(param1:Object) : void
        {
            var _loc_2:Class = null;
            var _loc_3:ResourceInfo = null;
            var _loc_4:Object = null;
            if (PreloaderConfig.preResName)
            {
                this._maxLength = PreloaderConfig.preResName.length;
            }
            for each (_loc_4 in param1)
            {
                
                _loc_2 = FileType.getLoaderInfoByType(_loc_4.type as String);
                if (_loc_2)
                {
                    _loc_3 = new _loc_2(_loc_4);
                }
                else
                {
                    _loc_3 = new ResourceInfo(_loc_4);
                }
                _loc_3.loaclPath = _loc_3.path;
                _loc_3.path = PathConst.mainPath + _loc_3.path;
                if (this._maxLength > this._length)
                {
                    this._index = PreloaderConfig.preResName.indexOf(_loc_3.name);
                    if (this._index != -1)
                    {
                        _loc_3.path = PreloaderConfig.preResUrl[this._index].path + "?v=" + ParamsConst.instance.preLoaderVersion;
                        _loc_3.loaclPath = _loc_3.path;
                        _loc_3.path = PathConst.mainPath + _loc_3.path;
                        var _loc_7:String = this;
                        var _loc_8:* = this._length + 1;
                        _loc_7._length = _loc_8;
                    }
                }
                ResourceManager.addResource(_loc_3);
            }
            return;
        }// end function

        protected function newWrite(param1:Object) : void
        {
            var _loc_3:Class = null;
            var _loc_4:ResourceInfo = null;
            var _loc_5:String = null;
            var _loc_6:int = 0;
            var _loc_7:Array = null;
            var _loc_8:String = null;
            var _loc_9:Object = null;
            var _loc_2:* = getTimer();
            if (PreloaderConfig.preResName)
            {
                this._maxLength = PreloaderConfig.preResName.length;
            }
            for (_loc_8 in param1)
            {
                
                _loc_7 = param1[_loc_8];
                for each (_loc_9 in _loc_7)
                {
                    
                    _loc_9.path = _loc_8 + "/" + _loc_9.name;
                    _loc_5 = _loc_9.name;
                    _loc_6 = _loc_5.lastIndexOf(".");
                    _loc_5 = _loc_5.slice(_loc_6);
                    _loc_9.type = _loc_5;
                    _loc_3 = FileType.getLoaderInfoByType(_loc_9.type as String);
                    if (_loc_3)
                    {
                        _loc_4 = new _loc_3(_loc_9);
                    }
                    else
                    {
                        _loc_4 = new ResourceInfo(_loc_9);
                    }
                    _loc_4.loaclPath = _loc_4.path;
                    _loc_4.path = PathConst.mainPath + _loc_4.path;
                    if (this._maxLength > this._length)
                    {
                        this._index = PreloaderConfig.preResName.indexOf(_loc_4.name);
                        if (this._index != -1)
                        {
                            _loc_4.path = PreloaderConfig.preResUrl[this._index].path + "?v=" + ParamsConst.instance.preLoaderVersion;
                            _loc_4.loaclPath = _loc_4.path;
                            _loc_4.path = PathConst.mainPath + _loc_4.path;
                            var _loc_14:String = this;
                            var _loc_15:* = this._length + 1;
                            _loc_14._length = _loc_15;
                        }
                    }
                    ResourceManager.addResource(_loc_4);
                }
            }
            Log.debug("解析assets.vas耗时", getTimer() - _loc_2);
            return;
        }// end function

        public function getInfoByName(param1:String) : ResourceInfo
        {
            return ResourceManager.getInfoByName(param1);
        }// end function

        public function getUrlByName(param1:String) : String
        {
            var _loc_2:* = ResourceManager.getInfoByName(param1);
            if (_loc_2)
            {
                return _loc_2.path;
            }
            return "";
        }// end function

        public function getEffectTemplateByName(param1:String) : String
        {
            if (!this._effectMap[param1])
            {
                return "";
            }
            return this._effectMap[param1];
        }// end function

        public function get allEffect() : Array
        {
            var _loc_2:String = null;
            var _loc_1:Array = [];
            for (_loc_2 in this._effectMap)
            {
                
                _loc_1.push(_loc_2);
            }
            return _loc_1;
        }// end function

        public function get allBoss() : Array
        {
            return this._allBoss;
        }// end function

        public function get allNpc() : Array
        {
            return this._allNpc;
        }// end function

        public static function get instance() : ResConfig
        {
            if (_instance == null)
            {
                _instance = new ResConfig;
            }
            return _instance;
        }// end function

    }
}
